package com.sihan.framework.etm.controller.business;

import com.framework.annotation.BusinessLog;
import com.framework.model.ApiResult;
import com.sihan.framework.etm.common.CacheOperatorInfo;
import com.sihan.framework.etm.common.CommonBiz;
import com.sihan.framework.etm.dto.business.*;
import com.sihan.framework.etm.enums.CacheKeyName;
import com.sihan.framework.etm.enums.MqttSendCommandFunc;
import com.sihan.framework.etm.enums.OperateType;
import com.sihan.framework.etm.service.business.CheckService;
import com.sihan.framework.etm.service.business.ScrapStandingService;
import com.sihan.framework.etm.vo.business.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Map;

@Api(tags = "检查与报废")
@RestController
@RequestMapping("/bs/v0")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ScrapStandingBookController {

	private final ScrapStandingService scrapStandingService;
	
	private final CommonBiz commonBiz;
	
	private final CheckService checkService;

	@ApiOperation(value = "检查台账", notes = "检查台账", response = ScrapStandingBookVO.class)
	@GetMapping("/check/checkBook")
	@BusinessLog(value = "检查台账")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true),
			@ApiImplicitParam(name = "id", value = "用户Id", dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "string", paramType = "query")
	})
	@RequiresPermissions("bs:check:standing")
	public ApiResult<?> selectCheckBookPage(ScrapStandingBookDTO dto){
		return ApiResult.success(scrapStandingService.selectStandingBookPage(dto));
	}

	@ApiOperation(value = "异常清单", notes = "异常清单", response = ExceptionListVO.class)
	@GetMapping("/scrap/exceptionList")
	@BusinessLog(value = "异常清单")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true),
			@ApiImplicitParam(name = "toolCode", value = "RFID编号", dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "deviceCode", value = "智能柜编号", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "model", value = "类型", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "string", paramType = "query")
	})
	@RequiresPermissions("bs:exception:list")
	public ApiResult<?> selectExceptionkListPage(ExceptionListDTO dto){
		return ApiResult.success(scrapStandingService.selectExceptionkListPage(dto));
	}

	@ApiOperation(value = "报废台账", notes = "报废台账", response = ScrapListVO.class)
	@GetMapping("/scrap/standingBook")
	@BusinessLog(value = "报废台账")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true),
			@ApiImplicitParam(name = "toolCode", value = "RFID编号", dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "deviceCode", value = "智能柜编号", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "model", value = "类型", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "string", paramType = "query")
	})
	@RequiresPermissions("bs:scrap:standing")
	public ApiResult<?> selectStandingBookPage(ExceptionListDTO dto){
		return ApiResult.success(scrapStandingService.selectScrapListPage(dto));
	}

	@ApiOperation(value = "工器具档案", notes = "工器具档案", response = Map.class)
	@GetMapping("/scrap/toolDetail")
	@BusinessLog(value = "工器具档案")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "scrapId", value = "报废Id", dataType = "String", paramType = "query", required = true),
	})
	public ApiResult<?> getScrapDetailInfo(String scrapId){
		return ApiResult.success(scrapStandingService.getScrapDetailInfo(scrapId));
	}

	@ApiOperation(value = "检验记录", notes = "检验记录", response = ToolCheckRecordVO.class)
	@GetMapping("/check/getToolCheckRecode")
	@BusinessLog(value = "检验记录")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true),
			@ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "toolId", value = "工器具ID", dataType = "string", paramType = "query", required = true)
	})
	public ApiResult<?> getToolCheckRecode(ToolCheckRecordDTO dto){
		return ApiResult.success(scrapStandingService.getToolCheckRecode(dto));
	}
	
	@ApiOperation(value = "验证是否有报废权限", notes = "验证是否有报废权限")
	@GetMapping("/scrap/permission/has")
	@BusinessLog("验证是否有报废权限")
	@RequiresPermissions("bs:scrap:list")
	public ApiResult<?> validateHasScrapPermission(){
		commonBiz.sendCommandToDevice(MqttSendCommandFunc.COMMAND_2000.getValue());
		return ApiResult.success();
	}

	@ApiOperation(value = "报废清单", notes = "报废清单", response = BaseToolInfo.class)
	@GetMapping("/getToolInfoList")
	@BusinessLog(value = "报废清单")
	@RequiresPermissions("bs:scrap:list")
	public ApiResult<?> getToolInfoList(ScrapReasonDTO dto){
		return ApiResult.success(scrapStandingService.getToolInfoList(dto));
	}

	@ApiOperation(value = "加入报废清单", notes = "加入报废清单")
	@PostMapping("/scrap/addScrapTable")
	@BusinessLog(value = "加入报废清单")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "toolId", value = "工器具Id", dataType = "String", paramType = "query", required = true),
			@ApiImplicitParam(name = "id", value = "异常编号", dataType = "String", paramType = "query", required = true)
	})
	public ApiResult<?> addScrapTable(@RequestBody AddScrapTableDTO addScrapTableDTO){
		scrapStandingService.addScrapTable(addScrapTableDTO.getToolId(), addScrapTableDTO.getId());
		return ApiResult.success();
	}

	@ApiOperation(value = "移除异常清单", notes = "移除异常清单")
	@GetMapping("/deleteExceptionTable")
	@BusinessLog(value = "移除异常清单")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "异常Id", dataType = "String", paramType = "query", required = true)
	})
	public ApiResult<?> deleteExceptionTable(String id){
		scrapStandingService.deleteExceptionTable(id);
		return ApiResult.success();
	}

	@ApiOperation(value = "移除报废清单", notes = "移除报废清单")
	@GetMapping("/scrap/getoutScrapList")
	@BusinessLog(value = "移除报废清单")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "toolId", value = "工器具对象数组", dataType = "String", paramType = "query", required = true)
	})
	public ApiResult<?> getoutScrapList(String toolId) throws IOException {
		scrapStandingService.getoutScrapList(toolId);
		return ApiResult.success();
	}

	@ApiOperation(value = "添加报废原因", notes = "添加报废原因")
	@PostMapping("/scrap/addScrapReason")
	@BusinessLog(value = "添加报废原因")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "toolId", value = "工器具Id", dataType = "String", paramType = "query", required = true),
			@ApiImplicitParam(name = "scrapReason", value = "报废原因", dataType = "String", paramType = "query", required = true)
	})
	public ApiResult<?> addScrapReason(@RequestBody ScrapReasonDTO dto) throws IOException {
		scrapStandingService.addScrapReason(dto);
		return ApiResult.success();
	}

	@ApiOperation(value = "完成报废添加", notes = "完成报废添加")
	@PostMapping("/scrap/completeScrap")
	@BusinessLog(value = "完成报废添加")
	public ApiResult<?> completeScrap(ScrapReasonDTO dto) throws IOException {
		CacheOperatorInfo.setMapValue(CacheKeyName.BF_TOOL_ID_LIST.getValue(), "tool_id");
		scrapStandingService.scrapSubmit();
		return ApiResult.success();
	}

	@ApiOperation(value = "开启扫描获取工器具信息", notes = "开启扫描获取工器具信息")
	@GetMapping("/scrap/getScanningToolInfo")
	@BusinessLog(value = "开启扫描获取工器具信息")
	public ApiResult<?> getScanningToolInfo() throws IOException {
		CacheOperatorInfo.setMapValue(CacheKeyName.READER_LOG.getValue(), "1");
		CacheOperatorInfo.setMapValue(CacheKeyName.BUSINESS_OPERATE_TYPE.getValue(),
				OperateType.SCRAP_SCAN.getValue());
		commonBiz.sendIvgScanStartCommand();
		return ApiResult.success();
	}
	
	@ApiOperation(value = "验证是否有检查权限", notes = "验证是否有检查权限")
	@GetMapping("/check/permission/has")
	@BusinessLog("验证是否有检查权限")
	@RequiresPermissions("bs:manager:check")
	public ApiResult<?> validateHasCheckPermission(){
		commonBiz.sendCommandToDevice(MqttSendCommandFunc.COMMAND_2000.getValue());
		return ApiResult.success();
	}
	
	@ApiOperation(value = "检查中", notes = "检查中", response = ApiResult.class)
	@GetMapping("/check/doing")
	@BusinessLog(value = "检查中")
	@RequiresPermissions("bs:manager:check")
	public ApiResult<?> checkDoing(){
		return checkService.checkDoing();
	}
	
	@ApiOperation(value = "完成检查", notes = "完成检查", response = ApiResult.class)
	@GetMapping("/check/done")
	@BusinessLog(value = "完成检查")
	public ApiResult<?> checkDone(){
		return checkService.checkDone();
	}

}
